﻿using System;
using System.Xml.Linq;
using IcicleFramework.Components.Behaviors;
using Microsoft.Xna.Framework;

namespace IcicleFramework.GameServices
{
    public class BehaviorFactory : IBehaviorFactory
    {
        public IBehavior GenerateBehavior(XElement element)
        {
            XAttribute classAttrib = element.Attribute("class"),
                        typeAttrib = element.Attribute("type");

            IBehavior behavior = null;

            if (classAttrib != null && typeAttrib != null)
            {
                string assembly;

                if (element.Attribute("assembly") != null)
                    assembly = element.Attribute("assembly").Value;
                else
                {
                    string[] temp = classAttrib.Value.Split('.');
                    assembly = temp[0];
                }

                Type classType = Type.GetType(String.Format("{0},{1}", classAttrib.Value, assembly));

                behavior = (IBehavior) Activator.CreateInstance(classType);
                behavior.Deserialize(element);
            }

            return behavior;
        }

        public void Update(GameTime gameTime) {}

        public void Initialize() {}
    }
}
